<?php

	// +----------------------------------------------------------------------
	// | 行为控制器
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\controller\admin;

	use app\common\event\Fanyi;
	use app\common\model\Lang as LangModel;
	use app\common\event\Lang as LangEvent;
	use yicmf\builder\Edit;
	use yicmf\builder\Table;
	use think\facade\Url;
	use app\controller\admin\Common;

	class Lang extends Common
	{
		protected function initialize()
		{
			parent::initialize();
		}

		public function _filter()
		{
			return [];
		}

		/**
		 * 行为管理
		 * @return mixed|string
		 * @author  : 微尘 <yicmf@qq.com>
		 */
		public function index()
		{
			// 显示页面
			$builder = new Table();
			$builder->model(LangModel::class)
				->where($this->_filter())
				->searchText('name', '标记')
				->searchText('zh_cn', '中文')
				->keyId('id', 'ID')
				->keyText('name', '标记')
				->keyText('zh_cn', '中文')
				->keyText('zh_hk', '繁体')
				->keyText('en_us', '英语')
				->keyUpdateTime()
				->keyStatus()
				->actionUpdate()
				->actionForbid()
				->actionRestore();
			return $builder->fetch();
		}


		public function update()
		{
			if ($this->request->isPost()) {
				parent::_update();
			} else {
				$id = $this->request->get('id');
				// 判断是否为编辑模式
				$isEdit = $id ? true : false;
				if ($isEdit) {
					$data = LangModel::where('id', $id)->find();
				} else {
					$this->_returnError('数据异常失败');
				}
				$builder = new Edit();
				$builder->title('编辑多语言' )

					->keyText('zh_cn', '中文')
					->keyText('zh_hk', '繁体')
					->keyText('en_us', '英语')
					->data($data)
					->buttonSubmit('', '保存')->buttonClose();
				return $builder->fetch();
			}
		}

		public function forbid()
		{
			$course = LangModel::where('id', $this->request->param('id'))->find();
			if ($course) {
				$course['status'] = -1;
				if ($course->save()) {
					$this->_returnSuccess('禁用成功');;
				} else {
					$this->_returnError('禁用失败');
				}
			} else {
				$this->_returnError('操作异常');
			}
		}

		public function restore()
		{
			$course = LangModel::where('id', $this->request->param('id'))->find();
			if ($course) {
				$course['status'] = 1;
				if ($course->save()) {
					$this->_returnSuccess('还原成功');;
				} else {
					$this->_returnError('还原失败');
				}
			} else {
				$this->_returnError('操作异常');
			}
		}

	}
